package com.pj.lib.auraServer.components.DB.tables;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import com.pj.lib.auraServer.components.DB.DB;
import com.pj.lib.auraServer.exceptions.AuraServerException;
import com.pj.lib.auraServer.utils.Log;
import com.pj.lib.aura.connection.UserModel;


public abstract class User implements TableInterface{

	private String TABLE_NAME = "LIB_USER";
	private String SEQUENCE_NAME = "USER_SEQUENCE";
	public static final String COLUMN_ID = "ID";
	public static final String COLUMN_NICK_NAME = "NICK_NAME";
	public static final String COLUMN_FIRST_NAME = "FIRST_NAME";
	public static final String COLUMN_LAST_NAME = "LAST_NAME";
	public static final String COLUMN_EMAIL = "EMAIL";
	public static final String COLUMN_PASSWORD = "PASSWORD";
	public static final String COLUMN_SOL = "SOL";
	public static final String COLUMN_IS_LOGGED = "IS_LOGGED";
	public static final String COLUMN_AURA_TYPE = "AURA_TYPE";
	
	protected DB db;
	
	@Override
	public String getTableName() {
		return TABLE_NAME;
	}

	@Override
	public void setTableName(String name) {
		TABLE_NAME = name;
	}
	
	@Override
	public String getTableSequence() {
		return SEQUENCE_NAME;
	}

	@Override
	public void setTableSequence(String name) {
		SEQUENCE_NAME = name;
	}

	@Override
	public void delete() throws AuraServerException, SQLException {
		db.exec((new StringBuilder("DROP TABLE "))
				.append(getTableName())
				.append(" IF EXISTS").toString());
	}
	
	@Override
	public void createIfNotExists(ArrayList<Map<String, Object>> dbTables) throws AuraServerException, SQLException {
		Boolean found = false;
		for(Map<String, Object> row : dbTables) {
			if(row.get("TABLENAME").equals(this.getTableName().toLowerCase()))
				found = true;
		}
		if(!found) {
			Log.i(this, "Creating table " + this.getTableName() + "...");
			this.create();
			Log.i(this,  this.getTableName() + " created.");
		}
		
	}
	
	public String createPasswordChecksum(String password, String sol) throws NoSuchAlgorithmException {
		password = sol + password;
		MessageDigest md = MessageDigest.getInstance("SHA-256");
		
		md.update(password.getBytes());
		
		byte[] mdbytes = md.digest();
		 
        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
          sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        
        System.out.println("hash: " + sb.toString());
        return sb.toString();
	}
	
	
	
	@Override
	public void setDB(DB db) {
		this.db = db;
		
	}
	
	

	@Override
	public String getDefaultOrder() {
		return " ORDER BY " + COLUMN_ID;
	}

	
	public abstract Long add(UserModel user) throws AuraServerException, SQLException;
	
	
	public abstract Boolean checkIfUserWithSpecifiedNameExist(String name) throws AuraServerException, SQLException;
	public abstract Long getSequence() throws AuraServerException, SQLException;
	
	public abstract Map<String, Object> getUserByLogin(String login) throws AuraServerException, SQLException;
	
	public abstract void loginUser(int userId) throws AuraServerException, SQLException;
	public abstract void unLoginUser(int userId) throws AuraServerException, SQLException;
	
}
